//
//  ChangeRecordViewController.m
//  MyWallet
//
//  Created by 小肥羊 on 15/9/11.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "ChangeRecordViewController.h"
#import "WalletLocalData.h"
#import "TotalAssets.h"

@interface ChangeRecordViewController ()

@end

@implementation ChangeRecordViewController

#pragma mark - Set
- (void)setRecordDate:(NSDate *)recordDate
{
    _recordDate = recordDate;
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString *locationString=[dateformatter stringFromDate:_recordDate];
    [self.changeDateButton setTitle:locationString forState:UIControlStateNormal];
}

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.selectDateView = [[SelectDateView alloc] initWithDelegate:self];
    [self.changeDateButton.layer setCornerRadius:kNomalRadius];
    [self.moneyFrom setText:self.receiveDict[@"MoneyFrom"]];
    [self.moneyText setText:[NSString stringWithFormat:@"%@",self.receiveDict[@"Money"]]];
    self.recordDate = self.receiveDict[@"RecordDtate"];
    self.selectDateView.datePicker.date = self.recordDate;
    [self.remarkText setText:self.receiveDict[@"Remarks"]];
    if ([self.receiveDict[@"CheckoutType"] isEqualToString:@"1"]) {
        self.moneyForSelectView.contents = [[NSMutableArray alloc] initWithArray:Macro_MoneyForSave];
    } else {
        self.moneyForSelectView.contents = [[NSMutableArray alloc] initWithArray:Macro_MoneyForDraw];
    }
    self.moneyForSelectView.delegate = self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    for (NSInteger i=0; i<self.moneyForSelectView.contents.count; i++) {
        if ([self.receiveDict[@"MoneyFor"] isEqualToString:self.moneyForSelectView.contents[i]]) {
            [self.moneyForSelectView selectAtIndex:i];
            self.moneyFor = self.receiveDict[@"MoneyFor"];
        }
    }
    if (!self.moneyFor) {
        [self.moneyForSelectView setHidden:YES];
        self.moneyFor = self.receiveDict[@"MoneyFor"];
    }
}

#pragma mark - 编辑结束
- (IBAction)finishEditTextFiled:(UITextField *)sender {
}

#pragma mark - 点击背景
- (IBAction)touchBackView:(UITapGestureRecognizer *)sender {
    [self.view endEditing:YES];
}

#pragma mark - 修改时间
- (IBAction)changeDate:(UIButton *)sender {
    [self.view endEditing:YES];
    [ActionView showView:self.selectDateView direction:PopViewDirectionStyleCenter];
}

#pragma mark - 时间选择代理
- (void)selectDate:(SelectDateView *)picker
{
    self.recordDate = picker.datePicker.date;
    [[NSNotificationCenter defaultCenter] postNotificationName:CLOESNOTICE object:picker];
}
- (void)cancelSelect:(SelectDateView *)picker
{
    [[NSNotificationCenter defaultCenter] postNotificationName:CLOESNOTICE object:picker];
}

#pragma mark - XFYSelectScrollView代理
- (void)operation:(XFYSelectScrollView *)selectView Button:(UIButton *)button AtIndex:(NSInteger)index
{
    [self.view endEditing:YES];
    self.moneyFor = button.titleLabel.text;
}

- (IBAction)changeRecord:(UIBarButtonItem *)sender {
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString *moneyFrom = self.moneyFrom.text;
    float money = [self.moneyText.text floatValue];
    float oldMoney = [self.receiveDict[@"Money"] floatValue];
    BOOL checkoutType = [self.receiveDict[@"CheckoutType"] isEqualToString:@"1"];
    if ([[WalletLocalData sharedManager] modifyRecord:self.receiveDict[@"RecordDtate"] WithNewDate:self.recordDate MoneyFrom:moneyFrom CheckoutType:checkoutType Money:money MoneyFor:self.moneyFor Remarks:self.remarkText.text]) {
        [[TotalAssets sharedManager] modifyRecord:moneyFrom ChangeMoney:(money - oldMoney) IsSave:checkoutType];
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        NSLog(@"失败");
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
